package com.jixiantubu.jinnang;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.JinNang_YouJi_Find_Adapter;
import com.jixiantubu.bean.Travelsbean;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

public class JinNang_YouJi_Find extends Activity implements OnClickListener {
	ListView findlListView;
	Button findButton;
	String somthing;
	private ImageView ivDeleteText;
	private EditText etSearch;
	List<Travelsbean> list = new ArrayList<Travelsbean>();
	JinNang_YouJi_Find_Adapter adapter;
	private String url = "http://" + MyApplication.getIP()
			+ ":8080/Jinnang/KePuTuiJian_Middle";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.jinnang_youji_find);
		initView();
		initData();

		// Download();
	}

	private void Download() {
		final CustomProgressDialog pd = new CustomProgressDialog(
				JinNang_YouJi_Find.this, "正在拼命加载数据,请稍等！", R.anim.frame);
		pd.show();
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", "1");
		params.addBodyParameter("title", somthing);
		System.out.println("游记输入的搜索内容" + somthing);
		//加载页面显示5秒，如果没有加载到数据  提示网络错误
		MyApplication.httpclient.configSoTimeout(5000);
		MyApplication.httpclient.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						pd.dismiss();
						MyApplication.show(JinNang_YouJi_Find.this, "加载数据失败，请检查网络!");
					}

					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {
						
						System.out.println("上传中  ");
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						pd.dismiss();
						String result = arg0.result;
                        	 Log.e("aaaaaa胡", result);
     						Gson gson = new Gson();
     						Type type = new TypeToken<List<Travelsbean>>() {
     						}.getType();
     						List<Travelsbean> list1 = gson.fromJson(result, type);
     						if (!(list1.size()==0)) {
     						list.addAll(list1);
     						Log.e("!胡蛟龙!", list.toString());
     						adapter.notifyDataSetChanged();
						}else {
							MyApplication.show(JinNang_YouJi_Find.this, "对不起，没有找寻到您所需要的内容，请从新输入");
						}
						
					}
				});
	}

	private void initView() {
		findlListView = (ListView) findViewById(R.id.jinnang_youji_find_lv);
		ivDeleteText = (ImageView) findViewById(R.id.ivDeleteText);
		etSearch = (EditText) findViewById(R.id.etSearch);
		findButton = (Button) findViewById(R.id.btnSearch);
		findButton.setOnClickListener(this);
		findlListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(JinNang_YouJi_Find.this,
						JinNang_YouJi_JuTiNeiRong.class);
				intent.putExtra("neirong", list.get(arg2));
				startActivity(intent);
			}
		});
	}

	private void initData() {
		ivDeleteText.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				etSearch.setText("");
			}
		});

		etSearch.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// somthing=etSearch.getText().toString().trim();

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// 得到输入框的值
				somthing = etSearch.getText().toString().trim();
				if (s.length() == 0) {
					ivDeleteText.setVisibility(View.GONE);
				} else {
					ivDeleteText.setVisibility(View.VISIBLE);
				}
				list.clear();

			}
		});

		// 初始化适配器
		adapter = new JinNang_YouJi_Find_Adapter(JinNang_YouJi_Find.this, list);
		adapter.setList(list);
		findlListView.setAdapter(adapter);

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btnSearch:
			Download();
			break;
		default:
			break;
		}

	}

}
